﻿using System;
using System.Collections.Generic;
using UnityEngine;


//Best 表示怪物难度最大，如此类推地设计
//这里的继承关系是指：MonsterBehaviorScoreModeBest 使用BehaviorModeType 级别的BehaviorMode
//这个BehaviorModeType 必须继承于使用MonsterBehaviorScoreModeBest 并且使用BehaviorModeType 级别的MonsterBehaviorMode

//整体来说就是，BehaviorModeType.behaviorScoreMode 属性的类型就是 MonsterBehaviorScoreModeBest<BehaviorModeType>
//MonsterBehaviorScoreMode<BehaviorModeType>.behaviorMode 属性的类型就是BehaviorModeType

public class MonsterBehaviorScoreModeBest<BehaviorModeType> : MonsterBehaviorScoreMode
    where BehaviorModeType : MonsterBehaviorMode
{  
    protected Dictionary<Func<int>, Action> behaviors = new Dictionary<Func<int>, Action>();
    protected Monster monster;
    protected BehaviorModeType behaviorMode;

    //各种怪兽行为的优先级
    protected int idlePriority = 1;
    protected int walkPriority = 2;
    protected int runPriority = 3;
    protected int jumpPriority = 4;
    protected int attackPriority = 5;

  


    protected virtual void Awake()
    {
        behaviorMode = GetComponent<BehaviorModeType>();
        behaviors.Add(IdleBehaviorScore, behaviorMode.IdleBehavior);
        behaviors.Add(WalkBehaviorScore, behaviorMode.WalkBehavior);
        behaviors.Add(RunBehaviorScore, behaviorMode.RunBehavior);
        behaviors.Add(JumpBehaviorScore, behaviorMode.JumpBehavior);
        behaviors.Add(NormalAttackBehaviorScore, behaviorMode.NormalAttackBehavior);
        monster = GetComponent<Monster>();
    }



    public override Action GetBehavior()
    {
        Action action = null;
        int maxScore = 0;
        List<Func<int>> scoreFuncs = new List<Func<int>>(behaviors.Keys);
        for (int i = 0; i != scoreFuncs.Count; ++i)
        {
            int result = scoreFuncs[i]();
            if (result == maxScore)
            {
                if (UnityEngine.Random.Range(0, 2) > 0)
                {
                    behaviors.TryGetValue(scoreFuncs[i], out action);
                    maxScore = result;
                }
            }
            else if (result > maxScore)
            {
                behaviors.TryGetValue(scoreFuncs[i], out action);
                maxScore = result;
            }
        }

        return action;
    }


    protected override int IdleBehaviorScore()
    {
        return idlePriority;
    }


    //walk 表示散步，即表示还没发现玩家的情况，所以究竟是随意散步还是Idle 就随机决定
    protected override int WalkBehaviorScore()
    {      
        return UnityEngine.Random.Range(0, 2) * walkPriority;
    }


    //run 表示发现敌人，朝敌人方向跑去
    protected override int RunBehaviorScore()
    {
        int score = 0;
        if (behaviorMode.GetHunchRangePlayers().Count > 0)
        {
            score += runPriority;
        }

        return score;
    }


    //在感应到敌人的时候，并且它的y 轴比怪物y 轴高时，怪物也应该跳跃(这样的话，下一次攻击命中玩家的几率更大)
    protected override int JumpBehaviorScore()
    {
        int score = 0;
        GameObject targetPlayer = behaviorMode.GetTargetPlayer();
        if (monster.jumpCount > 0 && targetPlayer != null && targetPlayer.transform.position.y > transform.position.y)
        {
            score += jumpPriority;
        }

        return score;
    }


    //能攻击到敌人的时候就攻击
    protected override int NormalAttackBehaviorScore()
    {
        int score = 0;
        GameObject targetPlayer = behaviorMode.GetAttackTargetPlayer();
        //在地面的时候处于IsInNormalAttackCD 的话可以发呆，但是不允许在空中发呆
        if (targetPlayer != null && (monster.IsGround || !monster.IsInNormalAttackCD()))
        {
            score += attackPriority;
        }
        return score;
    }
}
